//
//  SHEmoticonKeyboardView.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/29.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit

//表情键盘
class SHEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: CGRectMake(0, 0, SCREENW, 216))
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named:"emoticon_keyboard_background")!)
        
        addSubview(emoticonToolbar)
        
        addSubview(emoticonPageView)
        
        addSubview(pageControl)
        
        emoticonToolbar.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        
        emoticonPageView.snp_makeConstraints { (make) -> Void in
            
            //约束不全的话，会导致cell无法滑动
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emoticonToolbar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            
            make.bottom.equalTo(emoticonPageView).offset(5)
        }
        // 监听底部视图按钮点击回调
        
        emoticonToolbar.closure = {[weak self](type: SHEmoticonToolbarType) -> () in
            
            switch type{
            case .Default:
                print("默认")
            case .Emoji:
                print("Emoji")
            case .Lxh:
                print("浪小花")
            }
            // 获得indexPath
            let indexPath: NSIndexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            // 滚动emoticonPageView
            self?.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
                // 设置pageControl
            self?.setupPageControlData(indexPath)
        }
        
        let indexPath = NSIndexPath(forItem: 0, inSection: 0)
        
        setupPageControlData(indexPath)
    }
    
    
    
    //底部标签图
    // 底部视图
    private lazy var emoticonToolbar: SHEmoticonToolbar = SHEmoticonToolbar(frame: CGRectZero)
    
    //表情示意图
    private lazy var emoticonPageView:SHEmoticonPageView = {
        let view = SHEmoticonPageView()
        view.backgroundColor = self.backgroundColor

        view.delegate = self
        view.pagingEnabled = true
        
        view.bounces = false
        
        view.showsHorizontalScrollIndicator = false
        
        view.showsVerticalScrollIndicator = false
        return view
    }()
    
    private lazy var pageControl: UIPageControl = {
        
        let view = UIPageControl()
         // 在xcode 6.4 可以看到 以后苹果直接就给他隐藏了 但是你知道名字 可以通过kvc赋值
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        return view
    }()
    

}

extension SHEmoticonKeyboardView: UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 获取相对于与collectionView 中心点X
        let centerX = scrollView.contentOffset.x + SCREENW * 0.5
         // 拿到屏幕显示的cell 数组 对数组中元素进行排序
        let cells = self.emoticonPageView.visibleCells().sort{(cell1,cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
            
        }
        
        if cells.count == 2{
            let firstCell = cells.first!
            let lastCell = cells.last!
            var indexPath: NSIndexPath
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
                
                indexPath = self.emoticonPageView.indexPathForCell(firstCell)!
            }else{
                indexPath = self.emoticonPageView.indexPathForCell(lastCell)!
            }
            // 改变toolbar中按钮的状态
            emoticonToolbar.setupCurrentButton(indexPath.section)
            
            setupPageControlData(indexPath)
        }
    }
    
    func setupPageControlData(indexPath: NSIndexPath){
        
        pageControl.numberOfPages = SHEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
        
        pageControl.currentPage = indexPath.item
    }

}
